package cn.apthink.calendar;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import cn.apthink.utility.DateUtil;
import lombok.Getter;

public class DayTimeHelper {
	@Getter
	private final static Set<TimeRange> timeRanges = new HashSet<TimeRange>();
	@Getter
	private final static Set<Day> days = new HashSet<Day>();

	public static TimeRange getTimeRange(String startHM, String endHM, boolean isOvertime) {
		return getTimeRange(DateUtil.Time2Second(startHM), DateUtil.Time2Second(endHM), isOvertime);
	}

	public static TimeRange getTimeRange(int start, int end, boolean isOvertime) {
		Optional<TimeRange> o = timeRanges.stream().filter(x -> {
			return x.getStart() == start && x.getStart() == end;
		}).findFirst();
		if (o.isPresent()) {
			return o.get();
		} else {
			TimeRange tr = new TimeRange(start, end);
			tr.setOvertime(isOvertime);
			timeRanges.add(tr);
			return tr;
		}
	}

	public static TimeRange getTimeRange(WeekTemplateDetail d) {
		Optional<TimeRange> o = timeRanges.stream().filter(x -> {
			return x.getStart() == d.getStartSecond() && x.getStart() == d.getEndSecond();
		}).findFirst();
		if (o.isPresent()) {
			return o.get();
		} else {
			TimeRange tr = new TimeRange(d.getStartSecond(), d.getEndSecond());
			tr.setOvertime(d.isOvertime());
			timeRanges.add(tr);
			return tr;
		}

	}

	public static TimeRange getTimeRange(TimeRange t) {
		Optional<TimeRange> o = timeRanges.stream().filter(x -> {
			return x.equals(t);
		}).findFirst();
		if (o.isPresent()) {
			return o.get();
		} else {
			timeRanges.add(t);
			return t;
		}

	}

	public static Day getDay(Day day) {
		Optional<Day> o = days.stream().filter(x -> {
			return x.equals(day);
		}).findFirst();
		if (o.isPresent()) {
			return o.get();
		} else {
			days.add(day);
			return day;
		}
	}
}
